<template>
  <a-modal
      :confirmLoading="confirmLoading"
      :maskClosable="false"
      :title="title"
      :visible="visible"
      :width="720"
      @cancel="handleCancel"
      @ok="handleSubmit"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="应用名">
          <a-input
              v-decorator="['name', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]"
              placeholder="请输入应用名"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="应用标识">
          <a-input
              v-decorator="['appKey', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]"
              placeholder="请输入应用标识"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="开启状态">
          <a-radio-group
              v-decorator="[`status`, { initialValue: Status.ENABLED.key, rules: [{ required: true }] }]"
          >
            <a-radio
                v-for="(item, index) in Status.data"
                :key="index"
                :value="item.key"
            >{{ item.name }}
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="支付结果的回调地址">
          <a-input
              v-decorator="['orderNotifyUrl', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]"
              placeholder="请输入支付结果的回调地址"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="退款结果的回调地址">
          <a-input
              v-decorator="['refundNotifyUrl', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]"
              placeholder="请输入退款结果的回调地址"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="转账结果的回调地址">
          <a-input v-decorator="['transferNotifyUrl']" placeholder="请输入转账结果的回调地址"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
          <a-input v-decorator="['remark']" placeholder="请输入备注"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import * as Api from '@/api/pay/app'
import StatusEnum from '@/common/enum/Status'
import _ from 'lodash'

export default {
  data() {
    return {
      // 对话框标题
      title: '',
      // 标签布局属性
      labelCol: {
        span: 7
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),

      // 当前记录
      record: {}
    }
  },
  computed: {
    Status() {
      return StatusEnum
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.title = this.record && this.record.id ? '编辑应用' : '新增应用'
        this.$nextTick(() => {
          if (this.record && this.record.id) {
            this.setFieldsValue()
          } else {
            this.form.resetFields()
          }
        })
      }
    }
  },
  methods: {
    // 显示对话框
    show(record) {
      this.visible = true
      this.record = record || {}
    },

    // 设置默认值
    setFieldsValue() {
      const {$nextTick, form: {setFieldsValue}} = this
      $nextTick(() => {
        setFieldsValue(_.pick(this.record, ['name', 'appKey', 'status', 'orderNotifyUrl', 'refundNotifyUrl', 'transferNotifyUrl', 'remark']))
      })
    },

    // 确认按钮
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {form: {validateFields}} = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          this.onFormSubmit(values)
        }
      })
    },

    // 关闭对话框事件
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },
    // 提交到后端api
    onFormSubmit(values) {
      this.confirmLoading = true
      const params = this.record && this.record.id ? {...this.record, ...values} : values
      const api = this.record && this.record.id ? Api.edit(this.record.id, params) : Api.add(params)
      api.then(() => {
            this.$message.success('操作成功', 1.5)
            this.handleCancel()
            this.$emit('handleSubmit', params)
          })
          .finally(() => {
            this.confirmLoading = false
          })
    }
  }
}
</script>
